package com.activity;

import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.util.ExternalStorageUtil;
import com.util.ImageDisposeUtil;
import com.util.R;
import com.util.ToastUtil;

import java.io.File;

/**
 * Created by Administrator on 2016/3/30.
 */
public class ImageCompressActivity extends BaseActivity{
    private TextView tv_tip;
    private ImageView iv_compress;
    private long compressStartTime;
    private ProgressDialog dialog = null;
    private Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            if (msg != null) {
                if (msg.obj.equals("outOfSize")) {
                    ToastUtil.showBottomToast("图片过大,请重新选择");
                } else if (msg.obj.equals("ExternalStorageUnable")) {
                    ToastUtil.showBottomToast("压缩失败,请等待SD卡装载完成");
                } else if (msg.obj.equals("WriteFileFail")) {
                    ToastUtil.showBottomToast("压缩失败，手机存储不足");
                } else {
                    Bitmap bitmap = ImageDisposeUtil.getImageBitmap(msg.obj.toString());
                    File imgFile=new File(msg.obj.toString());
                    if (bitmap != null) {
                        tv_tip.setText("图片路径："
                                + msg.obj.toString()
                                + "   大小："
                                + imgFile.length()
                                / 1024
                                + "K   耗时："
                                + (System.currentTimeMillis() - compressStartTime)
                                / 1000 + "秒");
                        iv_compress.setImageBitmap(bitmap);
//                        if (!bitmap.isRecycled()) {
//                            bitmap.recycle();
//                        }
                    }
                }
                dialog.dismiss();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_compress);

        iv_compress = (ImageView) findViewById(R.id.iv_compress_imgCompress);
    }

    public void compressImg(View v) {
        showDialog();
        compressStartTime = System.currentTimeMillis();
        String sourcePath = ExternalStorageUtil.getExternalStoragePath() + "mytest" + File.separator;
        String filePath[] = new String[]{sourcePath + "test0-5.jpg",
                sourcePath + "test1.jpg", sourcePath + "test1-5.jpg", sourcePath + "test2.jpg",
                sourcePath + "test2-5.jpg", sourcePath + "test3.jpg", sourcePath + "test3-5.jpg",
                sourcePath + "test4.jpg", sourcePath + "test4-5.jpg", sourcePath + "test5.jpg",
                sourcePath + "test5-5.jpg", sourcePath + "test6.jpg", sourcePath + "test6-5.jpg",
                sourcePath + "test7.jpg", sourcePath + "test7-5.jpg", sourcePath + "test8.jpg"};
        ImageDisposeUtil.compressJPG(handler, filePath[9], 100);
    }

    private void showDialog() {
        if (dialog != null && !dialog.isShowing()) {
            dialog.show();
            return;
        } else if (dialog != null && dialog.isShowing()) {
            return;
        } else {
            dialog = new ProgressDialog(ImageCompressActivity.this);
            dialog.setTitle("图片压缩");
            dialog.setMessage("正在压缩...");
            dialog.setCancelable(false);
            dialog.show();
        }
    }
}
